<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="套装" prop="suitspid">
        <el-select v-model="queryParams.suitspid" placeholder="请选择套装" clearable style="width:100%">
          <el-option
            v-for="item in packs"
            :key="item.guid"
            :label="item.name"
            :value="item.guid"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['product:suitpack:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['product:suitpack:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:suitpack:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['product:suitpack:export']"
        >导出</el-button>
      </el-col>-->
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="suitpackList" @selection-change="handleSelectionChange">
      <el-table-column label="套装名称" align="center" prop="suitpack" width="220" />
      <el-table-column label="产品名称" align="center" prop="product" width="240" />
      <el-table-column label="数量" align="center" prop="amount" />
      <el-table-column label="单价" align="center" prop="price" />
      <el-table-column label="操作人" align="center" prop="operator" />
      <el-table-column label="创建时间" align="center" prop="createtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createtime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:suitpack:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['product:suitpack:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改套装定义对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :inline="true" >
        <el-form-item label="套装" prop="suitspid" label-width="80px">
          <el-select v-model="form.suitspid" placeholder="请选择套装" size="small"  :disabled="disabled" clearable>
            <el-option
              v-for="item in packs"
              :key="item.guid"
              :label="item.name"
              :value="item.guid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="品种" prop="productId" label-width="80px">
          <el-select v-model="productId" placeholder="请选择品种" size="small"  :disabled="disabled" clearable>
            <el-option
              v-for="item in products"
              :key="item.guid"
              :label="item.productname"
              :value="item.guid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="单品" prop="childspid" label-width="80px">
          <el-select v-model="form.childspid" placeholder="请选择单品" size="small"  :disabled="disabled" filterable clearable style="width: 473px">
            <el-option
              v-for="item in specs"
              :key="item.guid"
              :label="item.name"
              :value="item.guid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="数量" prop="amount" label-width="80px">
          <el-input v-model="form.amount" placeholder="请输入数量" size="small" clearable />
        </el-form-item>
        <el-form-item label="单价" prop="price" label-width="80px">
          <el-input v-model="form.price" placeholder="请输入单价" size="small" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSuitpack, getSuitpack, delSuitpack, addSuitpack, updateSuitpack, exportSuitpack, } from "@/api/product/suitpack";
import { findSpecificationList, findProductList } from "@/api/public";

export default {
  name: "Suitpack",
  components: {
  },
  data() {
    return {
      readonly:false,
      disabled:false,
      isHidden:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 套装定义表格数据
      suitpackList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        suitspid: null,
        childspid: null,

        amount: null,
        price: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        suitspid: [
          { required: true, message: "套装不能为空", trigger: "blur" }
        ],
        productId: [{
            validator: (rule, value, callback) => {
              // if (!this.productId) {
              //   callback(new Error('请选择品种'));
              // } else {
              //   callback();
              // }
              // if(this.productId){
                callback();
              }

          }],
        childspid: [
          { required: true, message: "单品不能为空", trigger: "blur" }
        ],
        amount: [
          { required: true, message: "数量不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "单价不能为空", trigger: "blur"},
          { pattern: /^\+?[1-9]\d*$/, message: '请输入大于0的正整数'}
        ],

      },
      packs: [],
      products: [],
      specs: [],
      productId: '',

    };
  },
  created() {
    this.getList();
    this.getPacks();
  },
  watch: {
    'productId': function(val) {
      this.getSpecs(val);
      this.form.childspid = '';
    }
  },
  methods: {
    getPacks(){
      findSpecificationList({flag: 2}).then(response => {
        this.packs = response.data;
      });
    },
    getProducts() {
      findProductList().then(response => {
        this.products = response.data;
        // console.log(response.data);
      });
    },
    getSpecs(productId) {
      findSpecificationList({productguid: productId}).then(response => {
          this.specs = response.data;

      });
    },
    /** 查询套装定义列表 */
    getList() {
      this.loading = true;
      listSuitpack(this.queryParams).then(response => {
        this.suitpackList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        suitspid: null,
        childspid: null,
        amount: null,
        price: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.guid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加套装单品";
      this.getProducts();
      this.disabled=false;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const guid = row.guid || this.ids
      getSuitpack(guid).then(response => {
        console.log(guid);
        this.form = response.data;
        // this.form.childspid = '';
        this.open = true;
        this.title = "修改套装单品";
        this.disabled=true;
      });

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let postData={
            guid:this.form.guid,
            amount:this.form.amount,
            price:this.form.price,
          }

          if (this.form.guid != null) {
            updateSuitpack(postData).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSuitpack(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const guids = row.guid || this.ids;
      this.$confirm('是否确认删除该数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSuitpack(guids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有套装数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSuitpack(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
